package com.icangya.admin;


import com.alibaba.fastjson.JSONObject;
import com.icangya.Utils.JSONUtils;
import com.icangya.domain.Banners;
import com.icangya.service.IBannerManageService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value = "/banners")
public class BannerController {

    @Autowired
    private IBannerManageService iBannerManageService;

//    @RequestMapping(value = "/upload",method = RequestMethod.POST)
//    public JSONObject uploadBanners(@RequestParam(value="file",required = false) MultipartFile file, HttpServletRequest request){
//        String res;
//        try{
//            res = iBannerManageService.saveNewBanner("",request,file);
//        }catch (Exception e){return JSONUtils.getRoot(-2,"内部错误","");}
//
//        if(res.equals("")){
//            return JSONUtils.getRoot(-1,"上传未成功","");
//        }
//        if(res.equals("10")){return JSONUtils.getRoot(-3,"图片格式问题","");}
//
//        return JSONUtils.getRoot(0,"",res);
//    }
//
//    @RequestMapping(value = "/delete",method = RequestMethod.GET)
//    public JSONObject deleteOne(@RequestParam(value="id") long id){
//
//        boolean res = iBannerManageService.deleteIt(id);
//        if(res){
//            return JSONUtils.getRoot(0,"success","");
//
//        }
//        return JSONUtils.getRoot(-1,"failed","");
//    }
//
//    @RequestMapping(value = "",method = RequestMethod.GET)
//    public JSONObject getAll(@RequestParam(value="page") int page){
//        List res = iBannerManageService.getAllBanner(page);
//        List<Banners> bns=(List<Banners>) res.get(0);
//        int total = (int) res.get(1);
//
//
//        JSONObject [] jsons = new JSONObject[10];
//
//        for(int i=0;i<jsons.length;i++){
//            JSONObject json=new JSONObject();
//            json.put("id",bns.get(i).getId());
//            json.put("name",bns.get(i).getFilename());
//            json.put("path",bns.get(i).getPath());
//            jsons[i]=json;
//        }
//
//
//
//        if(bns.isEmpty()){
//            return JSONUtils.getRoot(-1,"还未添加Banner","");
//        }
//        if(page > total || page < 1){
//            return JSONUtils.getRoot(-2,"超出范围","");
//        }
//
////        return JSONUtils.getRoot(0,"success",jsons,res.get(1));
//        return JSONUtils.getRoot(0,"success","");
//    }

    /**
     * 获取所有bannsers
     * @return
     */
    @RequestMapping(value = "",method = RequestMethod.GET)
    public JSONObject getAll() {

        List<Banners> bns = iBannerManageService.getAllBanner();
        if (bns.size() == 0) {
            return JSONUtils.getRoot(-1, "还未添加banners记录", "");
        }
        return JSONUtils.getRoot(0, "success", bns);
    }

    /**
     * 上传文件到服务器
     * @param request
     * @param file
     */
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public void addBanners(HttpServletRequest request,@RequestParam(value="file") MultipartFile file){
        iBannerManageService.saveNewBanner(request,file);
    }

    /**
     * 将刚刚上传的文件保存起来
     * @param request
     * @return
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public JSONObject saveToSql(HttpServletRequest request){
        //如果session中carPic为空，则返回失败
        if(request.getSession().getAttribute("carPic").equals("")){
            return JSONUtils.getRoot(-3,"请上传图片","");
        }
        String ui;
        //将session 中的carpic保存着图片文件系统的实际地址；如果不为空就将其保存到数据库，否则返回失败
        try {
            ui = (String) request.getSession().getAttribute("carPic");
        }catch (Exception e){
            e.printStackTrace();
            return JSONUtils.getRoot(-2,"请上传图片","");
        }
       Serializable res = iBannerManageService.saveToSql(ui);

        if((long)res > 0L){
            //保存成功后删除清空carPic
            request.getSession().setAttribute("carPic","");
            return JSONUtils.getRoot(0,"success","");
        }
        return JSONUtils.getRoot(-1,"failed","");
    }
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public JSONObject deleteIt(@RequestParam(value="id")long id){
        boolean res=iBannerManageService.deleteIt(id);
        if(res){
            return JSONUtils.getRoot(0,"success","");
        }
        return JSONUtils.getRoot(-1,"超出范围","");
    }

}
